package com.study.system.user.controller;


import com.alibaba.fastjson.JSON;
import com.study.system.BaseServlet;
import com.study.system.user.domain.User;
import com.study.system.user.domain.UserDept;
import com.study.system.user.service.UserService;
import com.study.system.user.service.impl.UserServiceImpl;


import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedReader;

/**
 * @Author 669
 * @CreateDate 2022/4/19 19:01
 * @Version 1.0
 */
@WebServlet("/user/*")
public class UserController extends BaseServlet {


    //修改个人信息
    public void updateInformation(HttpServletRequest req, HttpServletResponse resp) {
        try {
            //获取登录信息
            BufferedReader br = req.getReader();
            String params = br.readLine();
            // 将JSON字符串转为Java对象
            User user = JSON.parseObject(params, User.class);
            System.out.println(user);
            //修改用户信息
            UserService service = new UserServiceImpl();
            int i = service.updateUser(user);
            System.out.println(i);
            //用户存在响应为ture
            if (i == 1) {
                //修改成功！
                resp.getWriter().write("true");
            } else {
                //修改不成功
                resp.getWriter().write("false");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updatetojsp(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");

            UserDept userDept = new UserServiceImpl().selectallUserAndDeptById(Integer.parseInt(id));

            req.setAttribute("userDept", userDept);
            req.getRequestDispatcher("/jsp/admin/admin_updateInfromation.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //修改个人信息
    public void updateInformation2(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            //获取信息
            String name = req.getParameter("updatename");
            String id = req.getParameter("uid");
            String dept = req.getParameter("updatedept");

            int status = Integer.parseInt(req.getParameter("status"));


            User user = new User();
            user.setName(name);
            user.setId(Integer.parseInt(id));
            user.setDeptid(Integer.parseInt(dept));

            int i = new UserServiceImpl().updateUser(user);

            if (i == 1 && status == 0) {
                req.getRequestDispatcher("/jsp/login.jsp").forward(req, resp);
            }
            if (i == 1 && status == 1) {
                req.setAttribute("message", "修改成功！");
                req.getRequestDispatcher("/jsp/admin/admin_user.jsp").forward(req, resp);
            } else {
                req.setAttribute("message", "修改失败！");
                req.getRequestDispatcher("/jsp/user/user_index.jsp").forward(req, resp);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除个人信息
    public void delUser(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");

            int id = Integer.parseInt(req.getParameter("id"));

            UserServiceImpl service = new UserServiceImpl();
            int i = service.delUserById(id);
            if (i == 1) {
                req.setAttribute("message", "删除成功！");
                req.getRequestDispatcher("/jsp/admin/admin_user.jsp").forward(req, resp);
            } else {
                req.setAttribute("message", "删除失败！");
                req.getRequestDispatcher("/jsp/admin/admin_index.jsp").forward(req, resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

